import { defineStore } from "pinia";
import { reactive } from "vue";
import { useBasicInfoStore } from "./basicInfo";
import { getStorage, setStorage, toMap, toObj } from "@/utils";
import api from "@/api";

interface config {
  // token信息
  token: string
  alter: string
  // 装备信息
  equips:Record<'上衣'|'下装'|'头肩'|'腰带'|'鞋'|'手镯'|'项链'|'戒指'|'武器'|'公会勋章'|'团队模式勋章'|'冒险勋章'|'称号',{
    id:string
    reinforce:number
    enchant:string
    emblems :string[]
    seals:string[]
  }>
  skills:{
    skill:string
    tp:number
    lv:number
  }[]
}

export const useConfigStore = defineStore("config", () => {
  const state = reactive<config>({
    token: "",
    alter: "",
    skills:[],
    equips:{
      "上衣":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "下装":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "头肩":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "腰带":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "鞋":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "手镯":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "项链":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "戒指":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "武器":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "公会勋章":{
        id:"",
        reinforce:20,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "团队模式勋章":{
        id:"",
        reinforce:0,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "冒险勋章":{
        id:"",
        reinforce:0,
        enchant:"",
        emblems:[],
        seals:[]
      },
      "称号":{
        id:"",
        reinforce:0,
        enchant:"",
        emblems:[],
        seals:[]
      }
    }
  });

  const toRefsValue = toRefs(state)

  const load = async () => {
    const alter = state.alter;
    let data = await getStorage<Object>(`dcalc/${alter}/sets`, {});

    // 本地无配置的时候的处理
    // if (Object.keys(data).length == 0 || !data?.single_set || (data?.single_set ?? [])?.length == 0) {
    //   data = await api.defaultConfig(alter)
    // }
    const temp = toMap(data, ["equips"]) as any;
    Object.keys(temp).forEach((key) => {
      if (Object.keys(state).includes(key)) {
        state[key] = temp[key];
      }
    });
  };

  const save = () => {
    const alter = state.alter;
    const temp = toObj(state)
    Reflect.deleteProperty(temp, "token")
    setStorage(`dcalc/${alter}/sets`, temp);
  };

  const calc = async () => {
    if(state.token != "")
    {
      save();
      const res = await api.calc(toObj(state));
      return res
    }
    else{
      return   {
        basicInfo: [],
        skillInfo: [],
        token: "",
      }
    }
  };


  return {
    state,
    ...toRefsValue,
    load,
    save,
    calc,
  };
});
